package com.vf.admin.client.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppQianDaoRecord;
import com.vf.admin.utils.StringEx;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author anxiang
 * @version 1.0.0
 * @ClassName AppQianDaoRecordDao.java
 * @Description TODO
 * @createTime 2021年07月23日 19:04:00
 */
@Repository
public class AppQianDaoRecordDao extends BaseDao<AppQianDaoRecord> {


    public void saveOrUpdate(AppQianDaoRecord appQianDaoRecord) {
        if (appQianDaoRecord.getId() == null || appQianDaoRecord.getId() == 0) {
            appQianDaoRecord.setId(this.add(appQianDaoRecord));
        } else {
            this.update(appQianDaoRecord);
        }
    }

    public List<AppQianDaoRecord> htfindList(Map<String, Object> paramap, SplitPageBean splitPageBean) {
        List<Object> list = new ArrayList<>();
        StringBuilder str = new StringBuilder("");
        str.append("select * from app_qiandao_record where 1=1 ");
        if (StringEx.isNotNull(paramap.get("userid"))) {
            str.append(" and userid = ? ");
            list.add(paramap.get("userid"));
        }
        String sql = str.toString();

        return this.queryList(sql, list, AppQianDaoRecord.class, splitPageBean);
    }

    //获取用户当日签到
    public AppQianDaoRecord getNow(Integer userid) {
        StringBuilder str = new StringBuilder("");
        str.append("select * from app_qiandao_record where to_days(createtime) = to_days(now()) and userid = ? and isFamily = 0 and status = 1");
        List<Object> list = new ArrayList<>();
        list.add(userid);
        String sql = str.toString();
        return this.queryObj(sql, list, AppQianDaoRecord.class);
    }

    //获取用户昨日签到
    public AppQianDaoRecord getYesterday(Integer userid) {
        StringBuilder str = new StringBuilder("");
        str.append("select * from app_qiandao_record where TO_DAYS( NOW( ) ) - TO_DAYS( createtime ) <= 1 and userid = ? and isFamily = 0 and status = 1");
        List<Object> list = new ArrayList<>();
        list.add(userid);
        String sql = str.toString();
        return this.queryObj(sql, list, AppQianDaoRecord.class);
    }

    //获取家族当日签到
    public AppQianDaoRecord getFamilyNow(Integer userid) {
        StringBuilder str = new StringBuilder("");
        str.append("select * from app_qiandao_record where to_days(createtime) = to_days(now()) and userid = ? and status = 1 and isFamily=1");
        List<Object> list = new ArrayList<>();
        list.add(userid);
        String sql = str.toString();
        return this.queryObj(sql, list, AppQianDaoRecord.class);
    }

    //获取家族昨日签到信息
    public AppQianDaoRecord getFamilyYesterday(Integer userid) {
        StringBuilder str = new StringBuilder("");
        str.append("select * from app_qiandao_record where TO_DAYS( NOW( ) ) - TO_DAYS( createtime ) <= 1 and userid = ? and status = 1 and isFamily=1");
        List<Object> list = new ArrayList<>();
        list.add(userid);
        String sql = str.toString();
        return this.queryObj(sql, list, AppQianDaoRecord.class);
    }


    /**
     * 更新用户头像
     * @param userid
     * @param pic 头像地址
     */
    public void updateImg(Integer userid,String pic){
        if(StringEx.isNull(userid)){
            return ;
        }
        if(StringEx.isNull(pic)){
            return;
        }
        String sql = " UPDATE app_qiandao_record SET pic = ?  WHERE userid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(pic);
        paramList.add(userid);
        this.update(sql,paramList);
    }
    /**
     * 更新用户昵称
     * @param userid
     * @param nick 昵称
     */
    public void updateNick(Integer userid,String nick){
        if(StringEx.isNull(userid)){
            return ;
        }
        if(StringEx.isNull(nick)){
            return;
        }
        String sql = " UPDATE app_qiandao_record SET nick = ?  WHERE userid = ?  ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(nick);
        paramList.add(userid);
        this.update(sql,paramList);
    }

    public AppQianDaoRecord qiandaoList(Integer userId, String time,String time2) {
        StringBuilder str = new StringBuilder();
        List list = new ArrayList();
        str.append("select * from app_qiandao_record where userid = ?");
        list.add(userId);
        str.append(" and createtime >= ?");
        list.add(time);
        str.append(" and createtime <= ?");
        list.add(time2);
        String sql = str.toString();
        return this.queryObj(sql,list,AppQianDaoRecord.class);
    }

    public List<AppQianDaoRecord> list(Map<String, Object> paramap) {
        List<Object> list = new ArrayList<>();
        StringBuilder str = new StringBuilder("");
        str.append("select * from app_qiandao_record where status=2 ");
        if(paramap.containsKey("today") && StringEx.isNotNull(paramap.get("today"))) {
            str.append(" and to_days(createtime) = to_days(now())");
        }
        String sql = str.toString();

        return this.queryList(sql, list, AppQianDaoRecord.class);
    }
}
